{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cc6b7b9f-a75d-4b78-930f-26d2343ac2c2",
   "metadata": {},
   "source": [
    "# Lesson 3: Enable Logging"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06fc5839-0b4b-42a3-9aaf-99a97e347955",
   "metadata": {},
   "source": [
    "### Import all needed packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63cf31e4",
   "metadata": {
    "height": 98
   },
   "outputs": [],
   "source": [
    "import boto3\n",
    "import json\n",
    "import os\n",
    "\n",
    "bedrock = boto3.client('bedrock', region_name=\"us-west-2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5d9e31b",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "from helpers.CloudWatchHelper import CloudWatch_Helper\n",
    "cloudwatch = CloudWatch_Helper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87a30e7e",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "log_group_name = '/my/amazon/bedrock/logs'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2461a51c",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "cloudwatch.create_log_group(log_group_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0afe5997",
   "metadata": {
    "height": 267
   },
   "outputs": [],
   "source": [
    "loggingConfig = {\n",
    "    'cloudWatchConfig': {\n",
    "        'logGroupName': log_group_name,\n",
    "        'roleArn': os.environ['LOGGINGROLEARN'],\n",
    "        'largeDataDeliveryS3Config': {\n",
    "            'bucketName': os.environ['LOGGINGBUCKETNAME'],\n",
    "            'keyPrefix': 'amazon_bedrock_large_data_delivery',\n",
    "        }\n",
    "    },\n",
    "    's3Config': {\n",
    "        'bucketName': os.environ['LOGGINGBUCKETNAME'],\n",
    "        'keyPrefix': 'amazon_bedrock_logs',\n",
    "    },\n",
    "    'textDataDeliveryEnabled': True,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ca33ffe",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "bedrock.put_model_invocation_logging_configuration(loggingConfig=loggingConfig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41fdb20d",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "bedrock.get_model_invocation_logging_configuration()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95ebecb7",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "bedrock_runtime = boto3.client('bedrock-runtime', region_name=\"us-west-2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "402c6311",
   "metadata": {
    "height": 420
   },
   "outputs": [],
   "source": [
    "prompt = \"Write an article about the fictional planet Foobar.\"\n",
    "\n",
    "kwargs = {\n",
    "    \"modelId\": \"amazon.titan-text-express-v1\",\n",
    "    \"contentType\": \"application/json\",\n",
    "    \"accept\": \"*/*\",\n",
    "    \"body\": json.dumps(\n",
    "        {\n",
    "            \"inputText\": prompt,\n",
    "            \"textGenerationConfig\": {\n",
    "                \"maxTokenCount\": 512,\n",
    "                \"temperature\": 0.7,\n",
    "                \"topP\": 0.9\n",
    "            }\n",
    "        }\n",
    "    )\n",
    "}\n",
    "\n",
    "response = bedrock_runtime.invoke_model(**kwargs)\n",
    "response_body = json.loads(response.get('body').read())\n",
    "\n",
    "generation = response_body['results'][0]['outputText']\n",
    "\n",
    "print(generation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68a7bab7",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "cloudwatch.print_recent_logs(log_group_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1118acfb",
   "metadata": {},
   "source": [
    "To review the logs within the AWS console, please use the following link to reference the steps outlined in the video:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cf443d2",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "from IPython.display import HTML\n",
    "aws_url = os.environ['AWS_CONSOLE_URL']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b7c904c",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "HTML(f'<a href=\"{aws_url}\" target=\"_blank\">GO TO AWS CONSOLE</a>')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
